#if !defined(ReferenceHeader)
#define ReferenceHeader

#include <stdio.h>

#include "configuration.h"
#include "node.h"
#include "switcher.h"

class Trail;

// Initially unbinded, may then point to another node
class Reference : public Node {
public:
  static const NodeType ID = -1;
  NodeType type () const {
    if(target.node)
      return target.type ();
    else
      return ID;
  }

  std::string variableName () {
    char buffer[20];
    buffer[0] = sampleVariablePrefix;
    sprintf (buffer + 1, pointerFormat, (size_t) final ());
    return buffer;
  }

  virtual std::string dump (int precedence) {
    if(target.node)
      return target.node->dump (precedence);
    else
      return variableName ();
  }

  virtual std::string display (int precedence) {
    if(target.node)
      return target.node->display (precedence);
    else
      return variableName ();
  }

  virtual pNode replicate ();

  virtual bool doSwitch (Switcher &switcher) {
    if(target.node)
      return target.node->doSwitch (switcher);
    else
      return switcher.unbinded (*this);
  }

  virtual void bound (Node&, Trail&);
  virtual void unbound () {
    target = 0;
  }

  pNode target;
};

#endif
